package com.pet.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.pet.context.BaseContext;
import com.pet.dto.web.PetAdoptApplicationDTO;
import com.pet.entity.PetAdoptApplication;
import com.pet.entity.PetAdoptInfo;
import com.pet.entity.PetInformation;
import com.pet.exchange.PetAdoptApplicationUtil;
import com.pet.mapper.PetAdoptApplicationMapper;
import com.pet.mapper.PetAdoptMapper;
import com.pet.mapper.PetInformationMapper;
import com.pet.mapper.UserInformationMapper;
import com.pet.pageDto.PetAdoptApplicationPageDTO;
import com.pet.result.PageResult;
import com.pet.service.PetAdoptApplicationService;
import com.pet.vo.web.PetAdoptApplicationVO;
import com.pet.vo.web.PetAdoptInfoVO;
import com.pet.vo.web.PetInformationVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class PetAdoptApplicationServiceImpl implements PetAdoptApplicationService {

    @Autowired
    private PetAdoptApplicationMapper petAdoptApplicationMapper;
    @Autowired
    private PetInformationMapper petInformationMapper;
    @Autowired
    private UserInformationMapper userInformationMapper;
    @Autowired
    private PetAdoptApplicationUtil petAdoptApplicationUtil;
    @Autowired
    private PetAdoptMapper petAdoptMapper;

    /**
     * 添加宠物领养申请
     * @param petAdoptApplicationDTO
     */
    public void insertPetAdoptApplication(PetAdoptApplicationDTO petAdoptApplicationDTO) {
        log.info("petAdoptApplicationDTO:{}",petAdoptApplicationDTO);
        //创建实体
        PetAdoptApplication petAdoptApplication = new PetAdoptApplication();
        //赋值
        BeanUtils.copyProperties(petAdoptApplicationDTO, petAdoptApplication);
        //设置时间等
        petAdoptApplication.setApplicationTime(LocalDateTime.now());
        petAdoptApplication.setApplicationUser(Math.toIntExact(BaseContext.getCurrentId()));

        petAdoptApplicationMapper.insertPetAdoptApplication(petAdoptApplication);

    }

    /**
     * 查询宠物领养申请进度
     * @return
     */
    public List<PetAdoptApplicationVO> selectPetAdoptApplication() {
        //创建VO LIST 存储最后的结果
        List<PetAdoptApplicationVO> petAdoptApplicationVOList = new ArrayList<>();
        //获取申请实体数组
        List<PetAdoptApplication> petAdoptApplicationList =
                petAdoptApplicationMapper.selectPetAdoptApplication(BaseContext.getCurrentId());
        log.info("petAdoptApplicationList:{}",petAdoptApplicationList);
        //转换
        for (PetAdoptApplication adoptApplication : petAdoptApplicationList) {
            //创建Vo
            PetAdoptApplicationVO petAdoptApplicationVO = new PetAdoptApplicationVO();
            //赋值 申请表信息
            BeanUtils.copyProperties(adoptApplication, petAdoptApplicationVO);
            //获取宠物信息
            PetInformation petInformation = petInformationMapper.selectPetInfoById(Math.toIntExact(adoptApplication.getPetId()));
            if (petInformation != null){
                PetInformationVO petInformationVO = new PetInformationVO();
                BeanUtils.copyProperties(petInformation ,petInformationVO);
                petInformationVO.setVaccine(String.valueOf(petInformation.getVaccine()));
                petInformationVO.setSterilization(String.valueOf(petInformation.getSterilization()));
                petInformationVO.setInsectRepellent(String.valueOf(petInformation.getInsectRepellent()));
                petInformationVO.setCreateUser(userInformationMapper.selectName(petInformation.getCreateUser()));
                //将修改后的信息进行赋值
                petAdoptApplicationVO.setPetInformationVO(petInformationVO);
            }

            if(adoptApplication.getApplicationStatus() == 1){
                petAdoptApplicationVO.setApplicationStatus("申请成功");
            }else if (adoptApplication.getApplicationStatus() == 2){
                petAdoptApplicationVO.setApplicationStatus("申请失败，请查看失败原因");
            }else {
                petAdoptApplicationVO.setApplicationStatus("待领养");
            }

            petAdoptApplicationVO.setApplicationUser(userInformationMapper.selectName(adoptApplication.getApplicationUser()));

            petAdoptApplicationVOList.add(petAdoptApplicationVO);
        }
        log.info("petAdoptApplicationVOList:{}",petAdoptApplicationVOList);
        return petAdoptApplicationVOList;
    }

    /**
     * 删除宠物领养申请
     * @param id
     */
    public void deletePetAdoptApplication(Long id) {
        petAdoptApplicationMapper.deletePetAdoptApplication(id);
        return;
    }

    /**
     * 修改宠物领养申请
     * @param dto
     */
    public void updatePetAdoptApplication(PetAdoptApplicationDTO dto) {
        PetAdoptApplication e = new PetAdoptApplication();
        BeanUtils.copyProperties(dto,e);
        e.setApplicationTime(LocalDateTime.now());
        petAdoptApplicationMapper.updatePetAdoptApplication(e);
        return;
    }

    /**
     * 分页查询宠物领养申请
     * @param dto
     * @return
     */
    public PageResult PetAdoptApplicationInfoPage(PetAdoptApplicationPageDTO dto) {
        log.info("dto:{}",dto);
        Page<PetAdoptApplicationVO> page = new Page<>();
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        //使用dishPageQueryDTO作为参数，执行一个分页查询，并将查询结果（一个Page对象，其中包含了DishVO类型的元素）存储在page变量中。
        Page<PetAdoptApplication> entity = petAdoptApplicationMapper.pageQuery(dto);
        for (PetAdoptApplication petAdoptApplication : entity) {
            PetAdoptApplicationVO vo = petAdoptApplicationUtil.exchangePetAdoptApplicationUtil(petAdoptApplication);
            vo.setApplicationUser(userInformationMapper.selectName(petAdoptApplication.getApplicationUser()));
            page.add(vo);
        }
        log.info("{}",page);
        return new PageResult(page.getTotal(), page.getResult());
    }

    private PetInformationVO exchangePetInformation(PetInformation petInformation){
        PetInformation e = petInformation;
        PetInformationVO vo = new PetInformationVO();
        BeanUtils.copyProperties(e,vo);
        vo.setCreateUser(userInformationMapper.selectName(e.getCreateUser()));
        return vo;
    }
}
